package com.kq.myofficialaccount.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import java.util.Map;

/**
 * 利用 JackSon 完成序列化
 */
public class JsonUtil {
    /**
     * 将 Java 对象序列化为 XML
     */
    public static String getXmlString(Object message) throws JsonProcessingException {
        XmlMapper xmlMapper = new XmlMapper();
        xmlMapper.setPropertyNamingStrategy(PropertyNamingStrategy.PASCAL_CASE_TO_CAMEL_CASE);
        return xmlMapper.writeValueAsString(message);
    }

    /**
     * 将 JSON 对象转化为 Map
     */
    public static Map<String, String> getParamMap(String jsonString) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.readValue(jsonString,
                new TypeReference<>(){});
    }
}
